﻿using BaseData;
using BaseData.Exts;
using BaseLib.Helper;
using BaseUtility;
using System;
using System.Linq;
using System.Web;
namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// NewsAdminInfoView 的摘要说明
    /// </summary>
    public class NewsAdminInfoView : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            var action = (Enums.Action)Enum.Parse(typeof(Enums.Action), context.Request.QueryString["action"]);
            using (var ctx = new ContextExt())
            {
                var inf = new Ext_Form_News_Info_Node();
                if (action == Enums.Action.ActEdit || action == Enums.Action.ActView)
                {
                    var id = context.Request.QueryString["id"].ToInt(0);
                    var infos = (from q in ctx.Web_Base_News_Info select q);
                    if (infos.Any(w => w.PkId == id))
                    {
                        var info = infos.FirstOrDefault(w => w.PkId == id);
                        if (info != null)
                        {
                            info.Content = (info.Content ?? "").Replace("&lt;", "<").Replace("&gt;", ">");
                            var nodes = (from q in ctx.Web_Base_News_Node select q);
                            if (nodes.Any(w => w.PkId == info.NodeId))
                            {
                                var node = nodes.FirstOrDefault(w => w.PkId == info.NodeId);
                                if (node != null) inf.node = node;
                            }
                            inf.info = info;
                        }
                    }
                }
                context.Response.Write(inf.ObjectsToJson());
                context.Response.ContentType = "text/plain";
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}